<template>
  <div class="goods-info">
    <!-- 返回上一个页面 -->
    <span class="mui-icon mui-icon-back"
          @click="back"></span>
    <!-- 轮播图 -->
    <swipe :lunbotu="lunbotu"
           style="height:9.2rem"></swipe>
    <!-- 商品参数 -->
    <div class="goods-par">
      <h3>商品信息</h3>
      <div class="parameter-box">
        <h4>{{ title }}</h4>
        <p class="explain">{{ describe }}</p>
        <div class="stock-and-price">
          <span class="stock">库存数量：<i>{{ stock }} </i>件</span>
          <span class="price">价钱：<i>{{ price }}￥</i></span>
        </div>
      </div>
    </div>
    <!-- 添加购物车 -->
    <div class="add-cart">
      <!-- 子组件向父组件传numbox的输入框的值 绑定事件getSelectCount 通过this.$emit('getCount',参数)-->
      <!-- 父组件向子组件传值 设置最大值为库存的数量 (异步拿到的库存数量是undefined，可以通过watch监听拿到库存的数量)-->
      <p> 购买数量 ：<numbox @getCount="getSelectCount"
                :max="stock"></numbox>
      </p>
      <button type="button"
              class="mui-btn mui-btn-success"
              @click="add">加入购物车</button>
      <button type="button"
              class="mui-btn mui-btn-warning">立即购买</button>
    </div>
  </div>
</template>
<script>
import swipe from '../public/swipe'
import numbox from '../public/numbox'
export default {
  data () {
    return {
      id: this.$route.params.id,
      goodsInfo: null,
      lunbotu: null,
      title: null,
      describe: null,
      stock: null,
      price: null,
      selectedCount: 1
    }
  },
  methods: {
    back () {
      this.$router.go(-1)
    },
    getSelectCount (count) {
      this.selectedCount = count
    },
    add () {
      let goodsInfo = { // 定义加入购物车商品对象
        id: this.id,
        count: this.selectedCount,
        price: this.price,
        selected: true,
        title: this.title
      }
      this.$store.commit('addToCar', goodsInfo)
      this.$router.push('/cart') // 跳转到购物车页面
    }
  },
  components: {
    swipe,
    numbox
  },
  created () {
    this.$http.get('/categoryShouji').then(result => {
      let ID = result.body.find(ele => {
        return Number(ele.id) === Number(this.id)
      })
      // console.log(ID)
      this.goodsInfo = ID
      this.lunbotu = this.goodsInfo.info.url
      this.title = this.goodsInfo.title
      this.describe = this.goodsInfo.info.describe
      this.stock = this.goodsInfo.info.num
      this.price = this.goodsInfo.price
      // console.log(this.lunbotu)
    })
  }

}
</script>
<style lang="less" scoped>
body {
  padding-bottom: 0;
}
.goods-info {
  padding: 0 10rem / @baseFontSize;
  .mui-icon-back {
    position: absolute;
    left: 20rem / @baseFontSize;
    top: 20rem / @baseFontSize;
    z-index: 99;
    width: 60rem / @baseFontSize;
    height: 60rem / @baseFontSize;
    line-height: 60rem / @baseFontSize;
    text-align: center;
    background-color: rgba(0, 0, 0, 0.3);
    border-radius: 50%;
    color: white;
  }
  // .mint-swipe[data-v-7ce95c02] {
  //   height: 792rem / @baseFontSize!important;
  // }
  // .mint-swipe[data-v-52a9aa24] {
  //   height: 792rem / @baseFontSize;
  // }
  .goods-par {
    border: 1px solid #999;
    box-shadow: 0 0 3px #999;
    margin-top: 16rem / @baseFontSize;
    padding: 0 10rem / @baseFontSize 10rem / @baseFontSize;
    h3 {
      font-size: 30rem / @baseFontSize;
      padding: 5rem / @baseFontSize 0;
    }
    .parameter-box {
      h4 {
        font-size: 20rem / @baseFontSize;
        padding: 5rem / @baseFontSize;
      }
      .explain {
        font-size: 16rem / @baseFontSize;
        color: aqua;
      }
      .stock-and-price {
        span {
          display: block;
          font-size: 18rem / @baseFontSize;
          color: #999;
          i {
            font-style: normal;
            color: red;
          }
        }
      }
    }
  }
  .add-cart {
    border: 1px solid #999;
    box-shadow: 0 0 3px #999;
    border-radius: 5px;
    margin: 15rem / @baseFontSize 0;
    padding: 10rem / @baseFontSize 15rem / @baseFontSize;
  }
}
</style>
